Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/listings/[uid]/cost-estimate/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — coût par la méthode du coût d'une annonce analysée : lecture (GET) et recalcul aux coûts d'aujourd'hui (POST) — sans rappeler le modèle.43import { NextRequest, NextResponse } from "next/server";44import { AnalysisError, getById, getLatest, recalc } from "@/lib/cost/ai/service";45import { clientKey, rateLimit } from "@/lib/cost/cache";4647export const runtime = "nodejs";4849export async function GET(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {50 const { uid } = await params;51 const id = req.nextUrl.searchParams.get("id");52 const v = id ? getById(id) : getLatest(decodeURIComponent(uid));53 if (!v || v.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "aucune analyse" }, { status: 404 });54 if (!v.estimate) return NextResponse.json({ error: "aucune estimation (analyse en cours ou échouée)", status: v.status }, { status: 409 });55 return NextResponse.json({ analysisId: v.id, costSnapshotDate: v.costSnapshotDate, combined: v.combined, estimate: v.estimate }, { headers: { "Cache-Control": "no-store" } });56}5758export async function POST(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {59 const { uid } = await params;60 if (!rateLimit(`ai-recalc:${clientKey(req)}`, 20, 60_000)) return NextResponse.json({ error: "Trop de requêtes." }, { status: 429 });61 let body: { analysisId?: string } = {};62 try { body = await req.json(); } catch { /* corps vide permis */ }63 try {64 const cur = body.analysisId ? getById(body.analysisId) : getLatest(decodeURIComponent(uid));65 if (!cur || cur.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "aucune analyse" }, { status: 404 });66 const v = recalc(cur.id);67 return NextResponse.json(v, { headers: { "Cache-Control": "no-store" } });68 } catch (e) {69 if (e instanceof AnalysisError) return NextResponse.json({ error: e.message, code: e.code }, { status: e.status });70 return NextResponse.json({ error: (e as Error).message }, { status: 500 });71 }72}73